<?php defined('SYSPATH') OR die('No direct access allowed.');

/**
 * Post model
 *
 * @package  Forum
 *
 * @property Database $db
 */

class Forum_Post_Model extends ORM implements Acl_Resource_Interface {

	//protected $db = 'forum';
	protected $belongs_to = array('category'=>'forum_category', 'author'=>'forum_user');
	protected $has_many = array('forum_comments');
	protected $foreign_key = array('forum_comments'=>'post_id');
	protected $reload_on_wakeup = FALSE;
	protected $ignored_columns = array('captcha');

  public function  __construct($id = NULL) {
    parent::__construct($id);
  }

  public function validate(array & $array, $save = FALSE) {
    $array = Validation::factory($array)
			->pre_filter('trim')
			->add_rules('title', 'required', 'length[3,100]')
			->add_rules('text', 'required');

		if (!Captcha::instance()->promoted())
			$array->add_rules('captcha', 'required', 'Captcha::valid');

		return parent::validate($array, $save);
  }

	public function get_last_comment() {
		return ORM::factory('post')
			->where('category_id', $this->id)
			->orderby('created', 'DESC')
			->find();
	}

	public function get_resource_id() {
		return 'forum_post';
	}

	public function get_by_user($id) {
		return $this->where('author_id', $id)->find_all();
	}
}